package com.jquest.editor;

import com.jquest.layer.MapLayer;
import org.newdawn.slick.*;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Vector2f;
import com.jquest.data.Map;
import com.jquest.data.Tile;
import com.jquest.layer.TileLayer;
import com.jquest.misc.Tileset;
import com.jquest.io.TilesetLoader;

/**
 * Created by IntelliJ IDEA.
 * User: Johan
 * Date: 2009-dec-12
 * Time: 04:11:36
 * To change this template use File | Settings | File Templates.
 */
public class RenderCanvas extends BasicGame {

    Map map;
    TileLayer tileLayer;
    Editor editor;
    Tileset ts;

    public RenderCanvas(Editor parent) {
        super("Canvas");
        this.editor = parent;
    }

    public void init(GameContainer gc) {
        gc.setAlwaysRender(true);
        gc.setShowFPS(false);

        ts = new TilesetLoader().load("./res/tilesets/demo.zip");
        editor.getSidePanel().assignTileset(ts);
        map = new Map(15, 15);
        tileLayer = new TileLayer(map, ts);
        tileLayer.EDITOR_MODE = true;
        map.addLayer(tileLayer);
        editor.setPropertyObject(map);

    }

    public void newMap(int sizeX, int sizeY) {
        map = new Map(sizeX, sizeY);
    }

    public void addLayer(MapLayer layer) {
        map.addLayer(layer);
    }

    public void removeLayer(MapLayer layer) {
    }

    public void render(GameContainer gc, Graphics g) {
        map.render(g);
    }

    public void update(GameContainer gc, int time) {

        Input i = gc.getInput();
        Vector2f tileCoords = screenToTile(i.getMouseX(), i.getMouseY());

        int x = i.getMouseX();
        int y = i.getMouseY();

        if(validateMapCoords(x, y, map)) {
            editor.getStatusBar().setMessage("["+tileCoords.getX()+","+tileCoords.getY()+"]");
        } else {
            editor.getStatusBar().setMessage("[0,0]");
        }
        TileLayer tl = (TileLayer) map.getLayer(0);
        if(i.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
            int tileIdx = editor.getTileList().getSelectedIndex();
            if(tileIdx != -1) {
                if(validateMapCoords(x, y, map)) {
                    tl.set((int) tileCoords.getX(),(int) tileCoords.getY(), new Tile((int) tileCoords.getX(),(int) tileCoords.getY(), tileIdx, (byte) 1));
                }
            }
        }
        if(i.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON)) {         
                if(validateMapCoords(x, y, map)) {
                    tl.set((int) tileCoords.getX(),(int) tileCoords.getY(), null);
                }
        }
        //map.update(time);
    }

    public boolean validateMapCoords(int x, int y, Map map) {
        return map.getCanvasWidth() > x && map.getCanvasHeight() > y;
    }

    public Vector2f screenToTile(int x, int y) {
        return new Vector2f((int) Math.floor((x / Tile.TILE_WIDTH)),(int) Math.floor((y / Tile.TILE_HEIGHT)));
    }

    public Map getMap() {
        return map;
    }

}
